package org.dahuatech.ssh.action.organization;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.dahuatech.ssh.entity.organization.Organization;
import org.dahuatech.ssh.service.organization.OrganizationManager;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@InterceptorRefs({  
	@InterceptorRef("defaultInterceporStack")
}) 
public class OrganizationAction extends ActionSupport {
	
	private Organization entity;
	
	private String organiztionTreeJson;
	
	private List<Organization> organizationList;
	
	@Autowired
	private OrganizationManager organizationManager;

	@Override
	public String execute(){
		organiztionTreeJson=organizationManager.listAllOrganizationJson();
		organizationList=organizationManager.listAllOrganization();
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 编辑或者新增页面
	 */
	public String input(){
		return ActionSupport.INPUT;
	}
	
	/**
	 * 获取子部门
	 * @return
	 */
	@Action(value="organization-suborg")
	public String getSubOrganization(){
		Long pid=Long.valueOf(ServletActionContext.getRequest().getParameter("pid"));
		String json = organizationManager.listAllSubOrganizationJson(pid);
        try {
        	ServletActionContext.getResponse().flushBuffer();
        	ServletActionContext.getResponse().getOutputStream().write(json.getBytes("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
		return null;
	}

	public void setEntity(Organization entity) {
		this.entity = entity;
	}

	public Organization getEntity() {
		return entity;
	}

	public void setOrganiztionTreeJson(String organiztionTreeJson) {
		this.organiztionTreeJson = organiztionTreeJson;
	}

	public String getOrganiztionTreeJson() {
		return organiztionTreeJson;
	}

	public void setOrganizationList(List<Organization> organizationList) {
		this.organizationList = organizationList;
	}

	public List<Organization> getOrganizationList() {
		return organizationList;
	}

}
